package com.atguigu.lease.web.app.service.impl;


import com.aliyun.dysmsapi20170525.Client;
import com.atguigu.lease.common.aliyun.AliyunProperties;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private Client client;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 获取短信验证码
     * @param phone
     */
    @Override
    public void getCode(String phone) {
        String key = "APP:Login"+phone;

        //判断是否有短信验证码
        //有就提示
        if(redisTemplate.hasKey(key)){
            throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
        }



        //发送短信
        String code = null;
        try {
            code = AliyunProperties.sentSms(phone, client);
            redisTemplate.opsForValue().set(key,code,30,TimeUnit.DAYS);
        } catch (Exception e) {
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }
        //没有就存




    }

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        //判断传过来的code是否是空
        if (!StringUtils.hasLength(loginVo.getCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        String key = "APP:Login"+loginVo.getPhone();

        //从redies中获取验证码并校验
        String code = redisTemplate.opsForValue().get(key);
        //判断code是否为空
        if (code==null){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }
        //验证码校验
        if (!loginVo.getCode().equals(code)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }

        //查询数据库中是否有phone 有就直接返回jwt 没有就先创建用户再返回jwt
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getPhone,loginVo.getPhone());
        UserInfo userInfo = userInfoService.getOne(queryWrapper);
        if (userInfo==null){
            userInfo = new UserInfo();
            userInfo.setPhone(loginVo.getPhone());
            userInfo.setNickname("公寓："+loginVo.getPhone());
            userInfo.setStatus(BaseStatus.ENABLE);
            userInfo.setAvatarUrl("http://192.168.126.128:9000/lease/2024/09/20/1a0a0d78-6595-4fce-88bb-1d5a5c5856b92.jpg");
            userInfoService.save(userInfo);
        }

        return JwtUtil.createToken(userInfo.getId());
    }
}
